package com.ccs.loas;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SearchFunctionLoa {

	public void search(WebDriver driver, String search, String value) throws Exception {
		
		Miscellaneous m = new Miscellaneous();
		m.go_to_Loa(driver);
		
		WebElement loaNo = driver.findElement(By.xpath("//input[@id='loa-number']"));
		WebElement pinLastName = driver.findElement(By.xpath("//input[@id='pin-lastname']"));
		WebElement advancedSearch = driver.findElement(By.xpath("//a[contains(.,'Advance Search')]"));
		advancedSearch.click();		
		
		WebElement firstName = driver.findElement(By.xpath("//input[@id='first-name']"));
		WebElement middleName = driver.findElement(By.xpath("//input[@id='middle-name']"));
		WebElement male = driver.findElement(By.xpath("//input[@id='gender1']"));
		//WebElement female = driver.findElement(By.xpath("//input[@id='gender2']"));
		selectDropdown(driver);
		
		if(search=="number"){
			loaNo.sendKeys(value);
		}else if(search=="pin"){
			pinLastName.sendKeys(value);
		}else if(search=="firstName"){
			firstName.sendKeys(value);
		}else if(search=="middleName"){
			middleName.sendKeys(value);
		}
		
		male.click();
		driver.findElement(By.xpath("//input[@value='Search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='data']/tbody/tr"));
		int size = rows.size();
		System.out.println("Search Result is: "+size);
		
	}
	
	public String select(WebDriver driver, String select) throws Exception {
		
		//Logic to identify which action to apply
		//Is it EDIT, VIEW, REVIEW, SUBMIT TO SLMC?
		
		//1st Step is to check its current status
		String status = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[6]")).getText();
		status.replaceAll(" ", "");
		//System.out.println("Status is: "+status);
		
		//Select action
		Select loc = new Select(driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/select")));
		
		String selected="";
		if(select.equals("view")){
			selected="View LOA";
		}else if(select.equals("edit")){
			selected="Edit LOA";
		}else if(select.equals("review")){
			selected="Review LOA";
		}else if(select.equals("slmc")){
			selected="Submit to SLMC";
		}else{
			System.out.println("Action select of loa is invalid");
		}
		loc.selectByVisibleText(selected);
		
		driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[8]/input[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		return status;
	}
	
	public void selectDropdown(WebDriver driver) throws Exception{
		Select location = new Select(driver.findElement(By.xpath("//select[@id='location']")));
		location.selectByVisibleText("Global City");
		Select status = new Select(driver.findElement(By.xpath("//select[@id='loaStatus']")));
		status.selectByVisibleText("ALL");
	}
	public void clear(WebDriver driver) throws Exception {
		
		Miscellaneous m = new Miscellaneous();
		m.go_to_Loa(driver);
		
		WebElement advancedSearch = driver.findElement(By.xpath("//a[contains(.,'Advance Search')]"));
		advancedSearch.click();
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.elementToBeClickable(
		        By.xpath("//a[contains(.,'Back to Simple Search')]")));
		WebElement simpleSearch = driver.findElement(By.xpath("//a[contains(.,'Back to Simple Search')]"));
		simpleSearch.click();
		
		WebElement loaNo = driver.findElement(By.xpath("//input[@id='loa-number']"));
		loaNo.sendKeys("Random String");
		driver.findElement(By.xpath("//input[@id='reset']")).click();
		driver.findElement(By.xpath("//input[@value='Search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		String stringLoa = driver.findElement(By.xpath("//input[@id='loa-number']")).getAttribute("value");
		if(stringLoa.equals("") || stringLoa.isEmpty()){
			System.out.println("Clear button is OK");
		}else{
			System.out.println("Clear does not work in search");
			driver.findElement(By.xpath("//input[@id='gender1']")).click();
		}
		
		
		
		
	}

}
